package product

import (
	"API/client"
	"API/request"
	"API/response"
	"common/utils"
	"github.com/gin-gonic/gin"
	product "product_server/proto"
)

func ListProduct(c *gin.Context) {
	var req request.ProductList

	if err := c.ShouldBind(&req); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}

	list, err := client.ProductList(c, &product.ProductListRequest{
		PriceMin:    req.PriceMin,
		PriceMax:    req.PriceMax,
		IsHot:       req.IsHot,
		IsNew:       req.IsNew,
		TopCategory: req.TopCategory,
		Pages:       req.Pages,
		PagePerNums: req.PagePerNums,
		Keyword:     req.Keyword,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}

	response.CurrencySuccessResponse(c, "商品列表展示成功", map[string]interface{}{"ProductList": list})
}

func ProductInfo(c *gin.Context) {
	var req request.ProductInfoRequest
	// 修改：使用 ShouldBindQuery 处理 GET 请求的查询参数
	if err := c.ShouldBindQuery(&req); err != nil {
		response.CurrencyErrResponse(c, 400, err.Error())
		return
	}

	list, err := client.ProductInfo(c, &product.ProductInfoRequest{
		Id: req.Id,
	})
	if err != nil {
		response.CurrencyErrResponse(c, -1, utils.CutOutRpcError(err.Error()))
		return
	}

	response.CurrencySuccessResponse(c, "商品详情展示成功", map[string]interface{}{"ProductInfo": list})
}
